package com.qf.web;

import com.qf.pojo.Page;
import com.qf.pojo.Student;
import com.qf.service.StudentService;
import com.qf.service.impl.StudentServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @date 2022/3/28
 * @desc
 */
@WebServlet("/studentList")
public class StudentListServlet extends HttpServlet {


    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        //条件查询
        String s_name = req.getParameter("s_name");
        String s_address = req.getParameter("s_address");

        //把查询条件封装到对象中
        Student student = new Student();

        if(s_name!=null && (!s_name.equals(""))){
            // 传过来的查询条件不为空  则封装到对象中
            student.setName(s_name);

            req.setAttribute("s_name",s_name);
        }

        if(s_address!=null && (!s_address.equals(""))){
            // 传过来的查询条件不为空  则封装到对象中
            student.setAddress(s_address);

            req.setAttribute("s_address",s_address);
        }


        //分页查询
        String num = req.getParameter("pageNum");
        int pageNum = 1;
        if(num!=null){
            // 如果前端传过来了 页码  ，就使用传过来的值，如果没有传页码   就显示第一页
            pageNum = Integer.parseInt(num);
        }
        //查询学生列表

        StudentService studentService = new StudentServiceImpl();

        //List<Student> stuList = studentService.findStudents();

        Page page = new Page<Student>();
        //封装 页码
        page.setPageNum(pageNum);
        // 封装每页显示的记录数
        page.setPageSize(10);

        // 分页查询的方法
        //page = studentService.getPageData(page);
        page = studentService.getPageDataByCondition(page,student);

        //把查询结果放在域对象
        //req.setAttribute("students",stuList);

        req.setAttribute("page",page);
        // 转发到页面  在页面上展示数据
        req.getRequestDispatcher("/studentList.jsp").forward(req,resp);
    }
}
